/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.shooter.SpaceBuddie.Baddies;
import java.util.ArrayList;


/**
 *
 * @author Unni Vemanchery Mana
 */
public final class BulletManager {
 
    private Texture bullet;
    private final String bulletImage = "enemy-bullet.png";
    private Sprite bulletSprite;
    private int counter              = 0;
    private ArrayList<Bullet> pool   = new ArrayList<Bullet>();
    private int bulletCounter        = 5;
    int playerX, playerY             = 0;
    
    private SpriteExplode se;
    private Rectangle bounds;
    
    
    
    public BulletManager(){
      initialize();
    }
    
    public void initialize(){
      Texture.setEnforcePotImages(false);  
      bullet       = new Texture(Gdx.files.internal(bulletImage));
      bulletSprite = new Sprite(bullet);
      bulletSprite.setSize(5,5);
      for(int i  = 0; i<bulletCounter; i++){
        Bullet b = new Bullet();
        pool.add(b);
      }
      se = new SpriteExplode(null);  
      bounds = new Rectangle();
      bounds.height = bulletSprite.getHeight();
      bounds.width  = bulletSprite.getWidth();
    }
    
    public void getBullet(int x, int y) {
      if(counter > (bulletCounter- 1)){
         counter = 0;
         return;
      }
      Bullet b  = pool.get(counter);
      b.visible = true;
      b.x = x + 1;
      b.y = y;
      playerX = x;
      playerY = y;
      pool.set(counter, b);
      counter++;
     }
    
    public void drawBullet(SpriteBatch batch, Player player,SpaceBuddie spaceBuddie){
        se.drawSpriteExplosion(batch);
        for(int i = 0; i<pool.size(); i++){
            Bullet b = pool.get(i);
            if(b.y > 400){
              b.y = 0;
              b.visible = false;
            }else if(b.visible){
               bulletSprite.setPosition(b.x, b.y);
               bulletSprite.draw(batch);
               int j = 0;
               for(Baddies bad: spaceBuddie.getBuddies()){
                  if(CollisionManager.checkSimpleCollision((int)bad.getX(), (int)bad.getY(), b.x, b.y, bullet)){
                   //if(CollisionManager.checkSimpleCollison(bad.getBounds(), bounds)){
                     bad.visible = false;
                     se.setAnimated(true);
                     se.initialize();
                     se.updatePosition(bad.getX(), bad.getY());
                     bad.setX(-1);
                     bad.setY(-1);
                     spaceBuddie.updateSpaceBuddies(j, bad);
                     b.visible = false;
                 }
                  j++;
               }
               b.y = b.y + b.speed;
               bounds.y = b.y;
               bounds.x = b.x;
            }
        }
    }
    
    public Sprite getSprite(){
        return bulletSprite;
    }
    
    private class Bullet {
     private int x;
     private int y;
     public boolean visible = false;
     public int speed       = 15;
     public void setX(int x){
       this.x = x;
     }
     public void setY(int y){
       this.y = y;
     }
     public int getX(){
         return x;
     }
     public int getY(){
         return y;
     }
    }

    }
    
    
